﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SharedLib;
namespace Tracy
{
    public partial class TaskScheduler
    {
        public class Task
        {
            /// <summary>
            /// Event when Task is triggered.
            /// </summary>
            public event EventHandler<OnTaskTriggeredEventArgs> OnTaskTriggered;

            private string _taskname = "";
            /// <summary>
            /// Get Task Name
            /// </summary>
            public string Name
            {
                get { return _taskname; }
            }

            private Type _handler;

            public Type Handler
            {
                get { return _handler; }
            }

            private int _taskinterval = 5;
            /// <summary>
            /// Get Task Interval (Seconds)
            /// </summary>
            public int Interval
            {
                get { return _taskinterval; }
            }


            private double _remainingtime = 5;
            /// <summary>
            /// Get remaining time before next Task (Seconds)
            /// </summary>
            public double RemainingTime
            {
                get { return _remainingtime; }
            }

            /// <summary>
            /// Reset remaining time to Task interval
            /// </summary>
            public void ResetRemainingTime()
            {
                _remainingtime = Interval;
            }

            /// <summary>
            /// Elapses time span and triggers OnTaskTriggered event
            /// </summary>
            /// <param name="seconds">Time elapsed</param>
            /// <returns>Remaining time</returns>
            public double Elapse(double seconds)
            {
                _remainingtime -= seconds;
                if (RemainingTime <= 0.5)
                {
                    ResetRemainingTime();
                    TriggerTaskEvent();
                }
                return RemainingTime;
            }

            public void TriggerTaskEvent()
            {
                if (OnTaskTriggered != null)
                {
                    OnTaskTriggered.InvokeEvent(this, new OnTaskTriggeredEventArgs(this));
                }
                
            }

            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="taskname">Task Name</param>
            /// <param name="interval">Task Interval</param>
            public Task(string taskname, Type taskHandler, int interval) : this(taskname, taskHandler, interval, 0)
            {
            }

            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="taskname">Task Name</param>
            /// <param name="interval">Task Interval</param>
            /// <param name="initial">Initial wait time before first run</param>
            public Task(string taskname, Type taskHandler, int interval, int initial)
            {
                _taskname = taskname;
                _taskinterval = interval;
                _handler = taskHandler;
                _remainingtime = Convert.ToDouble(initial);
            }
        }

        /// <summary>
        /// Task List
        /// Use the name of the Task instance as the key of the list.
        /// </summary>
        public class TaskList : KeyedCollection<string, Task>
        {
            /// <summary>
            /// Get the key of the Task item
            /// </summary>
            /// <param name="item">Task item</param>
            /// <returns>Task name</returns>
            protected override string GetKeyForItem(Task item)
            {
                return item.Name;
            }


        }
    }

}
